package com.tangcco.lockwell.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.Inform;
import com.tangcco.lockwell.domain.InformUserInfo;
import com.tangcco.lockwell.domain.dto.inform.InformAddDto;
import com.tangcco.lockwell.domain.dto.inform.InformDetailDto;
import com.tangcco.lockwell.domain.dto.inform.InformDto;
import com.tangcco.lockwell.domain.dto.inform.InformUserInfoDto;
import com.tangcco.lockwell.service.InformService;
import com.tangcco.lockwell.service.InformUserInfoService;
import com.tangcco.lockwell.viewmodel.admin.Inform.AcceptInformVM;
import com.tangcco.lockwell.viewmodel.admin.Inform.FindInformsVm;
import com.tangcco.lockwell.viewmodel.admin.Inform.InformVM;
import com.tangcco.lockwell.viewmodel.admin.user.UserGroupVm;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.tangcco.lockwell.utility.DateToString.dateToString;

/**
 * 通知公告控制器
 * @author 李金昭
 *
 */
@RequestMapping("/api/admin/inform")
@RestController("AdminInformController")
public class InformController {

    @Resource
    private InformService informService;
    @Resource
    private WebContext webContext;
    @Resource
    private InformUserInfoService informUserInfoService;

    /**
     * 新增通知公告
     * @param informAddDto
     * @return
     */
    @RequestMapping("insertByTeacher")
    public RestResponse insertByTeacher(@RequestBody InformAddDto informAddDto){
        //TODO 如果是周测消息，根据学科生成试卷id,拼接到内容末尾
        if(informAddDto.getInformType()!=null &&informAddDto.getInformType() == 91){
            //调用service生成试卷id，传参为学科id
            //获得id后，根据不同的学科生成不同跳转链接
        }

        Inform inform = new Inform();
        inform.setSendUserId(webContext.getCurrentUser().getUserId());
        inform.setContent(informAddDto.getContent());
        inform.setInformType(informAddDto.getInformType());
        inform.setDeleted(0);
        informService.insertSelectiveNotNull(inform);
        if(CollectionUtil.isNotEmpty(informAddDto.getUserIds())){
            informAddDto.getUserIds().stream().distinct().forEach(userId -> {
                InformUserInfo i = new InformUserInfo();
                i.setInformId(inform.getInformId());
                i.setAcceptUserId(userId);
                i.setIsread(1);
                informUserInfoService.insertSelective(i);
            });

        }
        return RestResponse.ok(inform);
    }
    @RequestMapping("updateIsRead/{informId}")
    public RestResponse updateIsRead(@PathVariable Integer id){
        informService.updateIsRead(id);
        return RestResponse.ok();
    }


    @RequestMapping("selectSelective")
    public RestResponse selectSelective(@RequestBody InformVM informVM){
        PageInfo<InformVM> informPageInfo = informService.selectSelective(informVM);
        return RestResponse.ok(informPageInfo);
    }

    @RequestMapping("deleteByid/{informId}")
    public RestResponse deleteByPrimaryKey (@PathVariable Integer informId){
        informService.deleteByPrimaryKey(informId);
        return RestResponse.ok();

    }

    @RequestMapping("findInfos")
    public RestResponse findInfos(@RequestBody FindInformsVm findInformsVm){
        informService.indesert(findInformsVm.getRecord(),findInformsVm.getUids(),findInformsVm.getIsAll());
        return RestResponse.ok();
    }

    //查询详情
    @RequestMapping("/selectDetailForTeacher/{informId}")
    public RestResponse selectDetailForTeacher(@PathVariable Integer informId){
        InformDetailDto result = new InformDetailDto();
        List<InformDto> informDtoList = informService.selectDetailForTeacher(informId);
        if(CollectionUtil.isNotEmpty(informDtoList)){
            result = BeanUtil.copyProperties(informDtoList.get(0), InformDetailDto.class);
            List<InformUserInfoDto> userInfoDtoList = new ArrayList<>();
            informDtoList.stream().forEach(informDto -> {
                InformUserInfoDto userInfoDto = new InformUserInfoDto();
                userInfoDto.setAcceptUserName(informDto.getAcceptUserName());
                userInfoDto.setAcceptRealName(informDto.getAcceptRealName());
                userInfoDto.setIsread(informDto.getIsRead());
                userInfoDto.setAcceptUserId(informDto.getAcceptUserId());
                userInfoDtoList.add(userInfoDto);
            });
            result.setInformUserInfoList(userInfoDtoList);
        }
        return RestResponse.ok(result);
    }

    @RequestMapping("selectUsersGroup")
    private RestResponse selectUsersGroup(@RequestBody UserGroupVm  informVM){
        PageInfo<UserGroupVm> userGroupVmPageInfo = informService.selectUsersGroup(informVM);
        return RestResponse.ok(userGroupVmPageInfo);
    }

    @RequestMapping("/selectAcceptInform")
    public RestResponse selectAcceptInform(@RequestBody AcceptInformVM model){
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(model.getPageIndex(), model.getPageSize());
        List<AcceptInformVM> acceptInformVMS = informService.selectAcceptInform(model);
        PageInfo<AcceptInformVM> pageInfo = new PageInfo<>(acceptInformVMS);
        return RestResponse.ok(pageInfo);
    }


}
